<?php

namespace App\Http\Controllers\Api;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\MUser;
use App\Models\MUserCash;
use App\Models\MUsersCommission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends ApiController
{
    /**
     * @param Request $request
     * @return array
     */
    function users_info(Request $request){
        $user = $request->user;
       $commission = MUsersCommission::where(['user_id'=>$user['id']])->sum('money');
        $new_commission=MUsersCommission::where(['user_id'=>$user['id']])->orderBy('created_at','desc')->first();

         $list = [
             'ID'=>$user->account,
             'nickname'=>$user->nickname,
             'avatar'=>$user->avatar,
             'is_auth'=>$user->is_auth,
             'note'=>$user->note,
              'commission'=>$commission,
              'new_commission'=>$new_commission['money']??0
         ];


        $this->data = $list;

        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 用户提现列表
     * @param Request $request
     * @return array
     */
   function users_cash(Request $request){
        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;
        $user= $request->user;
        $this->data['total'] = MUserCash::where('user_id',$user->id)->count();
        $lists = MUserCash::where('user_id',$user->id)->offset($start_count)->limit($size)->get();
        $this->data['page']=$page;
        $this->data['size']=$size;
        $this->data['lists'] = $lists;
        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 提现编辑页面
     * @return array
     */
    function users_cash_edit(Request $request){
       $user = $request->user;
       $cash_message = DB::table('shop_setting')->where('name','earnings')->value('value');
       $lists = [
            'bank_user_name'=>$user->bank_user_name,
            'bank_type'=>$user->bank_type,
            'bank_open'=>$user->bank_open,
            'bank_card'=>$user->bank_card,
            'cash_money'=>$user->cash_money,
            'is_auth'=>$user->is_auth,
            'cash_message'=>$cash_message?$cash_message:''
        ];
       $this->data =$lists;
      return $this->ReturnMessage(200,getMessage(),$this->data);
   }

    /**
     * @param Request $request
     */
    function users_cash_save(Request $request){
        $user = $request->user;
        $cash_money = number_make($request->input('cash_money'),2);
        if(empty($cash_money)){
            return $this->ReturnMessage(-1,'提现金额不能为空');
        }

        if($cash_money >$user->cash_money ){
            return $this->ReturnMessage(-1,getMessage(7));
        }

        $user->cash_money -= $cash_money;
        $user->bank_user_name = $request->bank_user_name;
        $user->bank_type = $request->bank_type;
        $user->bank_open = $request->bank_open;
        $user->bank_card = $request->bank_card;
        $user->bank_user_name = $request->bank_user_name;
        $user->bank_update_time = time();
        $result1 = $user->save();

        $users_cash = new MUserCash();
        $users_cash->txnum = get_rand_str(10,0,1);//提现编号
        $users_cash->user_id = $user->id;
        $users_cash->price = $request->cash_money;
        $users_cash->bank_type = $request->bank_type;
        $users_cash->bank_open = $request->bank_open;
        $users_cash->bank_card = $request->bank_card;
        $users_cash->real_name = $request->bank_user_name; //真实姓名
        $result2 =  $users_cash->save();

        if(!$result1 || !$result2){
            return $this->ReturnMessage(-1,getMessage(5),$this->data);
        }
        return $this->ReturnMessage(200,getMessage(6),$this->data);
    }

    /**
     * @param Request $request
     * @return array 实名认证页面
     */
    function users_auth(Request $request){
        $user = $request->user;
        $lists = [
            'image'=>$user->image,
            'card_imgs'=>$user->card_imgs,
            'card_img'=>$user->card_img,
            'card'=>$user->card,
            'realname'=>$user->realname,
            'is_auth'=>$user->is_auth
        ];
        $this->data =$lists;
        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * @param Request $request
     * @return array 实名认证操作
     */
    function users_auth_save(Request $request){
       $user = $request->user;
        if($user->is_auth ==4 ||$user->is_auth ==2){
           return $this->ReturnMessage(200,'已经认证或者认证中,无需再认证');
       }

        $user->image = $request->image;
        $user->card_imgs = $request->card_imgs;
        $user->card_img = $request->card_img;
        $user->card = $request->card;
        $user->realname = $request->realname;
        $user->is_auth = 2;
        $result= $user->save();
        if(!$result ){
            return $this->ReturnMessage(-1,'保存失败',$this->data);
        }
        return $this->ReturnMessage(200,'保存成功',$this->data);
    }

}